package net.cctms.zhishu.webAppFramework.ui;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.widget.TextView;


import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;

import net.cctms.zhishu.R;
import net.cctms.zhishu.webAppFramework.contact.UrlName;
import net.cctms.zhishu.webAppFramework.gestureImage.GestureImagesAdapter;
import net.cctms.zhishu.webAppFramework.global.BaseFragmentActivity;
import net.cctms.zhishu.webAppFramework.model.PicEntity;
import net.cctms.zhishu.webAppFramework.utils.IntentUtil;
import net.cctms.zhishu.webAppFramework.utils.Log;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

/**
 * 类说明：图集放大缩小
 *
 * @author kangxb
 * @version 1.0
 * @date 2016/3/1
 */
@Route(path = "/zhishu/GestureImageActivity")
public class GestureImageActivity extends BaseFragmentActivity implements ViewPager.OnPageChangeListener{
    private static String TAG = GestureImageActivity.class.getSimpleName();

    public static final int FROM_ARTICLE_DETAIL = 0x1000;   //来源于头像
    public static final int FROM_PERSONNAL_ICON = 0x1001;   //来源于头像

   /* public static final String ENTITY_DATA = "entity_data";
    public static final String POSITION = "position";*/
   /* private ArrayList<PicEntity> picEntityList;
   private int pageFrom;
   private int position;*/

    private ViewPager mViewPager;
    private TextView currentPage;
   /* private static final String FROM = "from";*/
    @Autowired(name = "position")
    public int position ;
    @Autowired(name = "pageFrom")
    public int pageFrom ;
    @Autowired
    public ArrayList<PicEntity> picEntityList;
    /**
     * 启动自身
     */
    public static void openActivity(Context context, ArrayList<PicEntity> picEntityList, int position) {
        Log.e("backinfo", new Gson().toJson(picEntityList));
        Intent intent = new Intent(context, GestureImageActivity.class);
        intent.putParcelableArrayListExtra(UrlName.ENTITY_DATA, picEntityList );
        intent.putExtra(UrlName.POSITION, position);

        IntentUtil.startActivity((Activity) context, intent, R.anim.scale_in, 0);
    }
    /**
     * 启动自身
     */
    public static void openActivity(Context context, ArrayList<PicEntity> picEntityList, int position, int pageFrom) {
        Intent intent = new Intent(context, GestureImageActivity.class);
        intent.putParcelableArrayListExtra(UrlName.ENTITY_DATA, picEntityList );
        intent.putExtra(UrlName.POSITION, position);
        intent.putExtra(UrlName.FROM, pageFrom);
        IntentUtil.startActivity((Activity) context, intent, R.anim.scale_in, 0);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gesture_image);
        ARouter.getInstance().inject(this);
        initData();
        initView();
    }

    private void initView() {
        mViewPager = (ViewPager) findViewById(R.id.paintings_view_pager);
        currentPage = (TextView) findViewById(R.id.current_page_tv);
        // 最终页初始化第一页出来用来显示第一页页码。 不处理第一页是不会显示的
        if(pageFrom == FROM_ARTICLE_DETAIL){
            currentPage.setText( 1 + "/" + picEntityList.size());
        }


        Log.e("backinfo","gest:picEntityList:"+new Gson().toJson(picEntityList));
        GestureImagesAdapter gestureImagesAdapter = new GestureImagesAdapter( this, mViewPager, picEntityList,  pageFrom);
        mViewPager.setAdapter(gestureImagesAdapter);

        mViewPager.addOnPageChangeListener(this);
        mViewPager.setCurrentItem(position);

    }

    private void initData() {
        if(getIntent().getExtras()!=null){
            position = getIntent().getIntExtra(UrlName.POSITION,0);
            pageFrom = getIntent().getIntExtra(UrlName.FROM,0);
            picEntityList = getIntent().getParcelableArrayListExtra(UrlName.ENTITY_DATA);
        }

    }

    private void doFinish(){
        finish();
        overridePendingTransition(0, R.anim.scale_alpha_out);
    }

    @Override
    public void onBackPressed() {
        doFinish();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        // no-op
    }

    @Override
    public void onPageSelected(int position) {
        currentPage.setText( (1 + position) + "/" + picEntityList.size());
    }

    @Override
    public void onPageScrollStateChanged(int state) {
        // no-op
    }

}

